/*
 * @Author: https://github.com/WangEn
 * @Author: https://gitee.com/lovetime/
 * @Date:   2018-03-27
 * @lastModify 2018-3-28
 * +----------------------------------------------------------------------
 * | WeAdmin 表格table中多个删除等操作公用js
 * | 有改用时直接复制到对应页面也不影响使用
 * +----------------------------------------------------------------------
 */
layui.extend({
	admin: '{/}../../static/admin/static/js/admin'
});
layui.use(['laydate', 'jquery', 'admin'], function() {
	var laydate = layui.laydate,
		$ = layui.jquery,
		admin = layui.admin;
	//执行一个laydate实例
	laydate.render({
		elem: '#start' //指定元素
	});
	//执行一个laydate实例
	laydate.render({
		elem: '#end' //指定元素
	});
	/*启用-停用*/
	window.member_stop = function (obj,url,lock,id) {
		layer.confirm('确认要更改吗？', function(index) {
			$.ajax({
				type: 'POST',
				url: url,
				dataType: 'json',
				data:"lock="+lock+"&id="+id,
				success: function(data){
					if(data.code==1){
						if($(obj).attr('title') == '启用') {

							//发异步把用户状态进行更改
							$(obj).attr('title', '停用')
							$(obj).find('i').html('&#xe62f;');
							$(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
							layer.msg('已停用!', {
								icon: 1,
								time: 1000
							},function(){
								window.location.reload();
							});
							

						} else {
							$(obj).attr('title', '启用')
							$(obj).find('i').html('&#xe601;');
							$(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
							layer.msg('已启用!', {
								icon: 1,
								time: 1000
							},function(){
								window.location.reload();
							});	
							
						}
					}else{
						layer.msg(data.msg,{icon:2,time:1000},function(){
						});
					}
				}
			});	
			
		});
	}

	/*用户-删除*/
	window.member_del = function (id,url) {
		var id=id;
		var url=url;
		layer.confirm('删除须谨慎，确认要删除吗？',function(index){
			$.ajax({
				type: 'POST',
				url: url,
				dataType: 'json',
				data:"id="+id,
				success: function(data){
					if(data.code==1){
						layer.msg(data.msg,{icon:1,time:1000},function(){
							location.reload();
						});
					}else if(data.code==3){
						layer.confirm('权限中还有子权限是否需要全部删除？', function(index) {
							$.ajax({
								type: 'POST',
								url: url,
								dataType: 'json',
								data:"id="+id+"&force=1",
								success: function(data){
									if(data.code==1){
										layer.msg(data.msg,{icon:1,time:1000},function(){
											
										});
									}else{
										layer.msg(data.msg,{icon:2,time:1000},function(){
										});
									}
								}
							});	
						})
					}else{
						layer.msg(data.msg,{icon:2,time:1000},function(){
						});
					}
				}
			});		
		});
	}

	/*批量-删除*/
	window.delAll = function (url) {
		var data1 = tableCheck.getData();
		var url=url;
		layer.confirm('确认要删除吗？', function(index) {
			$.ajax({
				type: 'POST',
				url: url,
				dataType: 'json',
				data:"allid="+data1,
				success: function(data){
					if(data.code==1){
						layer.msg(data.msg,{icon:1,time:1000},function(){
							location.reload();
						});
					}else if(data.code==3){
						layer.confirm('权限中还有子权限是否需要全部删除？', function(index) {
							$.ajax({
								type: 'POST',
								url: url,
								dataType: 'json',
								data:"allid="+data1+"&force=1",
								success: function(data){
									if(data.code==1){
										layer.msg(data.msg,{icon:1,time:1000},function(){
											
										});
									}else{
										layer.msg(data.msg,{icon:2,time:1000},function(){
										});
									}
								}
							});	
						})
					}else{
						layer.msg(data.msg,{icon:2,time:1000},function(){
						});
					}
				}
			});	
		});
	}

	/*排序-修改*/
	window.sort_change = function (obj,url,id) {
			$.ajax({
				type: 'POST',
				url: url,
				dataType: 'json',
				data:"sort="+obj.value+"&id="+id,
				success: function(data){
						window.location.reload();
				}
			});	
	}
    window.zd=function(id,url)
  {
    $.ajax({
          type:'post',
          url:url,
          //form中所有值全传
          data:'id='+id,
          datatype:'json',
          success:function(result)
          {
              if(result.code==1)
              {
                  layer.msg(result.msg,{icon:1,time:1000},function()
                  {
                      //返回后刷新一下
                      window.location.reload();
                  });
              }
              else
              {
                  layer.msg(result.msg,{icon:2,time:1000},function()
                  {
                      
                  });
              }
          }
      })
  }
});